﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace app2
{
    public class Boll
    {
        public int x;  
        public int y;     //球坐标
        public int d;     //球直径
        public int mx;
        public int my;    //球速度
        public int clientheight;
        public int clientwidth;  //界面高和宽
        public Random rand;
        private bool inscreen = false; //球是否在界面之内
        public Boll() { }
        public Boll(int clientwidth, int clientheight,Random rand)
        {
            this.rand = rand;
            this.rand = new Random();   //这样使用随机数避免重复
            this.clientheight = clientheight;
            this.clientwidth = clientwidth;
            this.d = 20;
            this.x = (-1)*rand.Next(clientwidth);
            this.y = (-1)*rand.Next(clientheight);
            this.mx = rand.Next(5) + 1;
            this.my = rand.Next(5) + 1;
        }

        public void Move()
        {
            if (inscreen == false && x > 0 && y > 0 && x < this.clientwidth - this.d && y < this.clientheight - this.d)
                inscreen = true;
            if (inscreen == true)    //处理碰撞并反弹
            {
                if (x < 0 || x > this.clientwidth - this.d)
                {
                    mx *= -1;
                }
                if (y < 0 || y > this.clientheight - this.d)
                {
                    my *= -1;
                }
            }
            x += mx;
            y += my;
        }
    }
}
